package com.linghu.qqservice.service;

import com.linghu.qqcommon.Message;
import com.linghu.qqcommon.MessageType;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;

/**
 * @author 令狐荣豪
 * @version 1.0
 * 该类对应的对象和某个客户端保持通信
 */
public class ServerConnectClientThread extends Thread {

    private Socket socket;
    private String userId;//连接服务端的用户id

    public ServerConnectClientThread(Socket socket, String userId) {
        this.socket = socket;
        this.userId = userId;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {//这里线程处于run状态，可以发送，接收消息
        while (true){
            try {
                System.out.println("服务器端和客户端"+userId+"保持通讯，读取数据。。。");
                ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) ois.readObject();
                //后面会使用message，根据message的类型，做相应的业务处理
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                    //客户端要用户在线列表

                    System.out.println(message.getSender()+"要在线用户列表");

                    String onlineUser = ManagerClientThreads.getOnlineUser();
                    //返回message
                    //构建一个Message对象，返回给客户端
                    Message message2=new Message();
                    message2.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message2.setContent(onlineUser);
                    message2.setGetter(message.getSender());
                    //返回给客户端
                    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
                    oos.writeObject(message2);
                } else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                    //根据message获取getterid，然后得到对应的线程
                    ServerConnectClientThread serverConnectClientThread =
                            ManagerClientThreads.geterverConnectClientThread(message.getGetter());
                    //得到对应的socket的对象输出流，将message对象转发给指定的客户端
                    ObjectOutputStream oos =
                            new ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    oos.writeObject(message);//转发，提示如果客户不在线，可以保存到数据库，这样就可以实现离线留言
                } else if (message.getMesType().equals(MessageType.MESSAGE_TO_ALL_MES)) {
                    //需要遍历 管理线程的集合，把所有线程的socket得到，然后把message进行转发即可
                    HashMap<String, ServerConnectClientThread> hm =ManagerClientThreads.getHm();
                    Iterator<String> iterator = hm.keySet().iterator();
                    while (iterator.hasNext()) {
                        //取出在线人的id
                        String onLineUserId =  iterator.next();

                        if (!onLineUserId.equals(message.getSender())){//排除群发消息的这个用户
                            //进行转发message
                            ObjectOutputStream oos = new
                                    ObjectOutputStream(hm.get(onLineUserId).getSocket().getOutputStream());
                            oos.writeObject(message);

                        }
                    }
                } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)) {
                    //根据getterId 获取对应的线程，将message对象转发
                    ServerConnectClientThread serverConnectClientThread = ManagerClientThreads.geterverConnectClientThread(message.getGetter());
                    ObjectOutputStream oos = new
                            ObjectOutputStream(serverConnectClientThread.getSocket().getOutputStream());
                    //转发
                    oos.writeObject(message);


                } else if(message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)){
                    //客户端退出

                    System.out.println(message.getSender()+"退出");

                    //将这个客户端对应的线程，从集合中删除
                    ManagerClientThreads.removeServerConnectClientThread(message.getSender());
                    socket.close();
                    break;//退出线程
                }else{
                    System.out.println("其他类型的message暂时不处理");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
}
